/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"46C3C385-48C9-488A-AA68-1BE3FC4698D8"}
 */
var fCuit = null;

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"AD37260E-DCDF-452C-90B0-3FD003BBB156"}
 */
function procesoArba(event) {
	/** @type {plugins.file.JSFile}*/
	var $arch = plugins.file.showFileOpenDialog(1, null, false, new Array("TXT", "txt", "dat"));
	
	if (!$arch)
		return 1
		
	var fr = new Packages.java.io.FileReader($arch);
	var br = new Packages.java.io.BufferedReader(fr);
	
	var $clientes 		= obtenerDs('clientes', 'cliente_id')
	var $proveedores 	= obtenerDs('proveedor', 'proveedor_id')

	/** @type {String} */ 
	var linea = br.readLine() //Leo una linea del archivo
	while (linea != null) //Hasta que termine el archivo
	{
		//application.output(i)
		tratarLinea(linea, $clientes, $proveedores)
		linea = br.readLine() //Leo una linea del archivo
	}
	br.close();
	fr.close();
	globals.DIALOGS.showInfoDialog('Aviso','Proceso terminado con exito','OK')
	return 0
}

/**
 * @param {String} tabla
 * @param {String} campo_id
 * 
 * @properties={typeid:24,uuid:"B4D64E29-175D-466C-A0B1-CF719AC74370"}
 */
function obtenerDs(tabla, campo_id)
{
	var $query = 'select '+ campo_id + ', cuit from ' + tabla + ' where cuit is not null order by cuit'
	var $args = null
	var $ds = databaseManager.getDataSetByQuery('infocom',$query,$args,-1)
	
	return $ds
}

/**
 * @param {JSDataSet} ds
 *
 * @properties={typeid:24,uuid:"3D533557-B4DC-41A0-A40B-3375727E0A6C"}
 */
function obtenerCuits(ds)
{
	return ds.getColumnAsArray(2)
}

/**
 * @param {String} linea
 * @param {JSDataSet} clientes
 * @param {JSDataSet} proveedores
 * 
 * @properties={typeid:24,uuid:"7916C389-DE2D-436F-AA5D-2BC57A2054B1"}
 */
function tratarLinea(linea, clientes, proveedores)
{
	var $cuit = linea.substr(29,11)
	$cuit = procesarCuit($cuit)
	if (linea[0] == 'P')
	{
		var $pos = existe(obtenerCuits(clientes),$cuit)
		if ($pos != -1)
		{
			var $percep = utils.stringToNumber(linea.substr(47,4))
			var $id = clientes.getValue($pos+1,1)
			application.output('Pos Ds: '+ $pos)
			application.output('Id Cli : '+$id+ '// Cuit : ' + $cuit +' // Percep Nueva : '+$percep)
			cargarCliente($id, $percep)
		}
	}
	else
	if (linea[0] == 'R')
	{
		var $pos2 = existe(obtenerCuits(proveedores),$cuit)
		if ($pos2 != -1)
		{
			var $reten = utils.stringToNumber(linea.substr(47,4))
			var $id2 	= proveedores.getValue($pos2+1,1)
			application.output('Pos Ds: '+ $pos2)
			application.output('Id Prov : '+$id2+ '// Cuit : ' + $cuit +' // Reten : '+$reten)
			cargarProveeedor($id2, $reten)
		}
	}
}

/**
 * @param {Number} id
 * @param {Number} percep
 *
 * @properties={typeid:24,uuid:"B7B2642B-7A50-4B51-9E42-8D00EC70C6A1"}
 * @AllowToRunInFind
 */
function cargarCliente(id, percep)
{
	var $sql = 'UPDATE clientes SET percep_ib = ? WHERE cliente_id = ?'
	var $args = [percep, id]
	plugins.rawSQL.executeSQL('infocom', 'clientes', $sql, $args)
}

/**
 * @param {Number} id
 * @param {Number} reten
 *
 * @properties={typeid:24,uuid:"FED62161-0B6D-4694-B154-C7A2E5FFA475"}
 * @AllowToRunInFind
 */
function cargarProveeedor(id, reten)
{
	var $sql = 'UPDATE proveedor SET ib_porc_retencion = ? WHERE proveedor_id = ?'
	var $args = [reten, id]
	plugins.rawSQL.executeSQL('infocom', 'proveedor', $sql, $args)
}

/**
 * @param {Array} array
 * @param {String} cuit
 * 
 * @properties={typeid:24,uuid:"90230906-2208-40E5-8C77-3B25C97C65B9"}
 */
function existe(array, cuit)
{
	var inf 	= 0
	var sup 	= array.length -1 
	var centro	= null
	
	while(inf <= sup)
	{
		centro = Math.round((sup + inf) / 2)
		if(array[centro] == cuit) 
			return centro;
		else
		{
			if (cuit < array [centro] )
				sup = centro - 1;
			else
				inf = centro + 1;
		}
	}
	return -1;
}

/**
 * @param {String} cuit
 *
 * @properties={typeid:24,uuid:"9B569D7F-B36C-4117-A87F-1EFBC52789E6"}
 */
function procesarCuit(cuit)
{
	return cuit.substr(0,2) + '-' + cuit.substr(2,8) + '-' + cuit.substr(10,1)
}
